package top.v5it.japi.plus.common.func;

import java.util.Objects;
import java.util.function.BiConsumer;

/**
 * 自定义函数接口
 *
 * @author zhanpu
 * @date 2018/12/18
 */
@FunctionalInterface
public interface VoidFunc2<T, U, E extends Throwable> {


    /**
     * Performs this operation on the given arguments.
     *
     * @param t the first input argument
     * @param u the second input argument
     * @throws E 异常
     */
    void accept(T t, U u) throws E;

    /**
     * 处理往外抛异常
     *
     * @param after
     * @return
     */
    default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after) {
        Objects.requireNonNull(after);

        return (t, u) -> {
            try {
                accept(t, u);
                after.accept(t, u);
            } catch (Throwable e) {
                throw new FuncException(e);
            }
        };
    }

    /**
     * 基于本函数返回{@link FuncException}
     *
     * @param t 参数t
     * @param u 参数u
     */
    default void acceptWithFuncException(T t, U u) {
        try {
            accept(t, u);
        } catch (Throwable e) {
            throw new FuncException(e);
        }
    }
}

